<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Service extends Default_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->_layout = "templates/default/layout";
	}
	public function Index(){
		$this->headTitle("Dịch vụ - Truyền hình cáp Đồng Tháp");
		
		$this->load->model('ServicePack_Model');
		$servicepack = $this->ServicePack_Model->getServicePackAll();
		$this->loadData('servicepack',$servicepack);
		//var_dump($servicepack);die;
		
		$this->load->model("Posts_Model");
		$thongbaomoi = $this->Posts_Model->getPostTop(1,6);
		$this->loadSidebar("thongbaomoi",$thongbaomoi);
		
		$tintucmoi = $this->Posts_Model->getPostTop(1,3);
		$this->loadSidebar("tintucmoi",$tintucmoi);
		$this->loadSidebar("fanpage");		
		$this->loaddata("news_hot",$this->Posts_Model->getPostTop(1,6));
		$this->loadView('default/service/service');
	}
	public function Detail(){
		$this->headTitle("Chi tiết dịch vụ");		
		$this->loadBreadcrumb("Trang chủ");
		$this->loadBreadcrumb("Dịch vụ");
		$this->loadBreadcrumb("Gói cơ bản");
		$this->load->model("Posts_Model");
		$thongbaomoi = $this->Posts_Model->getPostTop(1,6);
		$this->loadSidebar("thongbaomoi",$thongbaomoi);
		$this->loaddata("news_hot",$this->Posts_Model->getPostTop(1,6));
		$tintucmoi = $this->Posts_Model->getPostTop(1,3);
		$this->loadSidebar("tintucmoi",$tintucmoi);
		$this->loadSidebar("fanpage");
		$spid = $this->input->get("spid",true);
		$this->load->model('ServicePack_Model');
		$servicepack = $this->ServicePack_Model->getServicePack($spid);
		$servicepackdetail = $this->ServicePack_Model->getServicePackDetailAll($spid);
		$this->loadData('servicepack',$servicepack);
		$this->loadData('servicepackdetail',$servicepackdetail);
		$this->loaddata("listChannel",$this->ServicePack_Model->getListChannel($spid));
		//var_dump($this->ServicePack_Model->getListChannel($spid));die;
		
		$this->loadView('default/service/detail');
	}
	public function Order(){
		$this->_layout = "templates/default/order";
		$this->loadScript(base_url('js/jquery.validate.js'));
		$this->loadScript(base_url('js/localization/messages_vi.js'));
		$this->loadScript(base_url('js/jquery.order.js'));
		$this->headTitle("Chi tiết dịch vụ");
		
		$this->loadSidebar("thongbaomoi");
		//$this->loadSidebar("fanpage");		
		$this->loadBreadcrumb("Trang chủ");
		$this->loadBreadcrumb("Dịch vụ");
		$this->loadBreadcrumb("Gói cơ bản");
		$spdid = $this->input->get("detail",true);
		$this->load->model('ServicePack_Model');
		//$servicepack = $this->ServicePack_Model->getServicePack($spid);
		$servicepackdetail = $this->ServicePack_Model->getServicePackDetail($spdid);
		$this->load->model('PayMethod_Model');
		$paymethod=$this->PayMethod_Model->getAllMethod();
		$this->loadData('servicepackdetail',$servicepackdetail);
		$this->loadData('paymethod',$paymethod);
		
		if(!$this->session->userdata('state_login_default')){
			if($this->input->post("newcust_submit",true)){
				$this->load->model('Customers_Model');
				$this->load->model('Orders_Model');
				$cusid = $this->Customers_Model->insertCustomer();
				$id = $this->Orders_Model->insertOrder($cusid);
				redirect('default/service/viewinvoice?id='.$id);
			}
			if($this->input->post("login_submit",true)){
				$this->load->model('Customers_Model');
				if(!$this->Customers_Model->checkLogin()){
					$message = "Đăng ký dịch vụ không thành công! Email hoặc mật khẩu không đúng.";
					$this->loadData("message", $message);
				}else{
					
					$this->load->model('Orders_Model');
					$cusid = $this->session->userdata('UserId');
					$id = $this->Orders_Model->insertOrder($cusid);
					redirect('default/service/viewinvoice?id='.$id);
				}
				
				
				
			}
		}else{
			$this->load->model('Customers_Model');
			$cusid = $this->session->userdata('UserId');
			$customers = $this->Customers_Model->getCustomerByCusId($cusid);
			$this->loadData("customers", $customers);
			if($this->input->post()){
				$this->load->model('Orders_Model');
				$id = $this->Orders_Model->insertOrder($cusid);
				redirect('default/service/viewinvoice?id='.$id);
			}
		}
		$this->loadView('default/service/order');
	}
	function Viewinvoice(){
		$this->_layout = "templates/default/viewinvoice";
		$this->headTitle("Hóa đơn dịch vụ - Truyền hình cáp Đồng Tháp");	
		$this->load->model('PayMethod_Model');
		$paymethod=$this->PayMethod_Model->getAllMethod();
		$this->loadData('paymethod',$paymethod);
		$id = $this->input->get("id",true);
		$this->load->model('Orders_Model');
		$order=$this->Orders_Model->getOrderByOrderDetail($id);
		$this->loadData('order',$order);
		//var_dump($order); die;
		if($this->input->post()){
			$id = $this->input->get("id",true);
			$this->load->model('Orders_Model');
			$this->Orders_Model->updateOrderPayMethod($id,$this->input->post("paymethod",true));
			redirect('default/service/viewinvoice?id='.$id);
		}
		
		$this->loadView('default/service/viewinvoice');
	}
}